﻿@model ActivityLogSearchModel
@using Telerik.Web.Mvc;
@{   
    ViewBag.Title = T("Admin.Configuration.ActivityLog.ActivityLog").Text;
}

@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <i class="fa fa-fire"></i>
            @T("Admin.Configuration.ActivityLog.ActivityLog")
        </div>
        <div class="options">
			<button type="submit" id="delete-selected" name="delete-selected" value="delete_selected" class="btn btn-secondary">
				<i class="far fa-trash-alt"></i>
				<span>@T("Admin.Common.DeleteSelected")</span>
			</button>
            <button type="submit" name="clearall" value="clearall" class="btn btn-danger">
				<i class="far fa-trash-alt"></i>
				<span>@T("Admin.Common.DeleteAll")</span>
			</button>
        </div>
    </div>
	
	<div class="row mt-3 grid-filter">
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.CreatedOnFrom)
			@Html.EditorFor(model => Model.CreatedOnFrom, new { @class = "form-control" })
		</div>
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.CreatedOnTo)
			@Html.EditorFor(model => model.CreatedOnTo, new { @class = "form-control" })
		</div>
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.ActivityLogTypeId)
			@Html.DropDownListFor(ModelBase => ModelBase.ActivityLogTypeId, Model.ActivityLogType, T("Common.Unspecified"))
		</div>
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.CustomerEmail)
			@Html.TextBoxFor(model => model.CustomerEmail, new { @class = "form-control" })
		</div>
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.CustomerSystemAccount)
			@Html.EditorFor(model => model.CustomerSystemAccount, new { @class = "form-control" })
		</div>

		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabel(string.Empty, string.Empty)
			<button type="submit" id="btnSearch" value="btnSearch" class="btn btn-secondary btn-block">
				<span>@T("Admin.Common.ApplyFilter")</span>
			</button>
		</div>
	</div>

	<div class="mt-2">
		@(Html.Telerik().Grid<ActivityLogModel>()
			.Name("activityLog-grid")
			.DataKeys(x =>
			{
				x.Add(y => y.Id).RouteKey("Id");
			})
			.ClientEvents(events => events.OnDataBinding("onDataBinding").OnDataBound("onDataBound"))
			.Columns(columns =>
			{
				columns.Bound(x => x.Id)
					.ClientTemplate("<input type='checkbox' name='checkedRecords' value='<#= Id #>' class='checkboxGroups'/>")
					.Title("<input id='mastercheckbox' type='checkbox'/>")
					.Width(50)
					.HtmlAttributes(new { style = "text-align:center" })
					.HeaderHtmlAttributes(new { style = "text-align:center" });
				columns.Bound(x => x.ActivityLogTypeName);
				columns.Bound(x => x.Comment);
				columns.Bound(x => x.CustomerId)
					.ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Customer/Edit/") + "<#= CustomerId #>\"><#= CustomerEmail #></a>");
				columns.Bound(x => x.IsSystemAccount)
					.ClientTemplate("<i class='fa fa-fw icon-active-<#= IsSystemAccount #> mr-1'></i><#= SystemAccountName #>")
					.Centered()
					.Width(220);
				columns.Bound(x => x.CreatedOn)
					.Width(200);
			})
			.DataBinding(dataBinding =>
			{
				dataBinding.Ajax()
					.Select("ListActivityLogs", "ActivityLog");
			})
			.Pageable(settings => settings.PageSize(Model.GridPageSize).Position(GridPagerPosition.Both))
			.PreserveGridState()
			.EnableCustomBinding(true))
	</div>
    <script type="text/javascript">
		var selectedIds = [];

        $(document).ready(function () {

            $('#btnSearch').click(function () {
            	refreshGrid();
                return false;
            });

        	//"delete selected" button
            $('#delete-selected').click(function (e) {
            	e.preventDefault();

            	var postData = {
            		selectedIds: selectedIds
            	};

            	$.ajax({
            		cache: false,
            		type: "POST",
            		url: "@(Url.Action("DeleteSelected", "ActivityLog"))",
            		data: postData,
            		complete: function (data) {
            			refreshGrid();
            		},
            		error:function (xhr, ajaxOptions, thrownError){
            			alert(thrownError);
            		},
            		traditional: true
				});
				return false;
            });

        	$('#mastercheckbox').click(function () {
        		$('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
        	});

        	//wire up checkboxes.
        	$(document).on('change', '#activityLog-grid input[type=checkbox][id!=mastercheckbox]', function (e) {
        		var $check = $(this);
        		if ($check.is(":checked") == true) {
        			var checked = jQuery.inArray($check.val(), selectedIds);
        			if (checked == -1) {
        				//add id to selectedIds.
        				selectedIds.push($check.val());
        			}
        		}
        		else {
        			var checked = jQuery.inArray($check.val(), selectedIds);
        			if (checked > -1) {
        				//remove id from selectedIds.
        				selectedIds = $.grep(selectedIds, function (item, index) {
        					return item != $check.val();
        				});
        			}
        		}
        		updateMasterCheckbox();
        	});

        	function refreshGrid() {
        		var grid = $('#activityLog-grid').data('tGrid');
        		grid.currentPage = 1; //new search. Set page size to 1
        		grid.ajaxRequest();
        		//clear selected checkboxes
        		$('.checkboxGroups').attr('checked', false).change();
        		selectedIds = [];
        	}
        });

        function onDataBinding(e) {
        	var searchModel = {
        		ActivityLogTypeId: $('#ActivityLogTypeId').val(),
                CreatedOnFrom: $('#@Html.FieldIdFor(model => model.CreatedOnFrom)').val(),
                CreatedOnTo: $('#@Html.FieldIdFor(model => model.CreatedOnTo)').val(),
        		CustomerEmail: $('#@Html.FieldIdFor(model => model.CustomerEmail)').val(),
        		CustomerSystemAccount: $('#@Html.FieldIdFor(model => model.CustomerSystemAccount)').val()
            };
            e.data = searchModel;
        }

		function onDataBound(e) {
			$('#activityLog-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
				var currentId = $(this).val();
				var checked = jQuery.inArray(currentId, selectedIds);
				//set checked based on if current checkbox's value is in selectedIds.
				$(this).attr('checked', checked > -1);
			});

			updateMasterCheckbox();
		}

		function updateMasterCheckbox() {
			var numChkBoxes = $('#log-grid input[type=checkbox][id!=mastercheckbox]').length;
			var numChkBoxesChecked = $('#log-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
			$('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
		}
   </script>    
}